﻿function ConvertFile() {
    $.ajax({
        method: "post",
        url: "/Home/ConvertPPt",
        success: function (data) {
            alert(data.message);
        }
    });
}
var navMenuList = "";
var pptIndex = 0;
var ppts = [];
var pptContainerWidth = 0;
var pptContainerHeight = 0;
var pptRatio = 1; //ppt的比例
$(function () {
    //获取ppt的列表
    $.ajax({
        method: "post",
        url: "/Home/GetPPTResult",
        success: function (data) {
            if (data.length <= 0) {
                return;
            }
            ppts = data;
            var html = $('#NavTemplate').render(data);
            $('#NavMenuList').html(html);
            showFrame();
        }
    });
    pptContainerWidth = $('#pptFile').width();
    pptContainerHeight = $('#pptFile').height();

    //图标切换
    $(".ctrl_bar img").mouseover(function () {
        var index = $(this).attr('data-index');
        $(this).attr("src", "/Content/img/" + index + "a.png")
    })
    $(".ctrl_bar img").mouseout(function () {
        var index = $(this).attr('data-index');
        $(this).attr("src", "/Content/img/" + index + ".png")
    })
});
/*
   设置Div的大小
 */
function SetDivSize(width, height) {
    $('#pptFile').width(width);
    $('#pptFile').height(height);
}
/*
   设置Svg容器的
*/
function SetSvgContainer() {
    
    let divRatio = 1;

    let GetDivRatio = function () {
        let height = $('#pptFile').height();
        let width = $('#pptFile').width();
        divRatio = (width / height).toFixed(4);
    }();


    let GetPPTRatio = function () {
        var viewBox = $('#svgUrl svg').attr("viewBox");
        if (viewBox) {
            if (viewBox.split(" ").length == 4) {
                let pptWidth = viewBox.split(" ")[2];
                let pptHeight = viewBox.split(" ")[3];
                pptRatio = (pptWidth / pptHeight).toFixed(4);
            }
        }
    }();

    if (parseFloat(pptRatio) >= parseFloat(divRatio)) { //以高为准
        $("#svgUrl svg").css("height", "auto");
    }
    else { //以宽为准
        $("#svgUrl svg").css("width", "auto");
    }
}

/*
   上一页
 */
function up() {
    if (pptIndex > 0) {
        pptIndex -= 1;
    } else {
        pptIndex = 0;
    }
    showFrame();
}
/*
   下一页
 */
function down() {
    if (pptIndex >= ppts.length) {
        pptIndex = ppts.length;
    }
    else {
        pptIndex += 1;
    }
    showFrame();
}
/*
   显示ppt
 */
function showFrame() {
    if (ppts.length <= 0) {
        return;
    }
    $("#svgUrl").load(ppts[pptIndex].svg);

    $('#RemarkDiv').html(ppts[pptIndex].remark);
    if (remarkVisbile) {
        RemarkVisible();
    }

    setTimeout(function () {
        $('#svgUrl svg').removeAttr("width");
        $('#svgUrl svg').removeAttr("height");
        SetSvgContainer();
        var svgElement = document.getElementById("svgUrl");
        for (var i = 0; i < svgElement.getElementsByTagName("a").length; i++) {
            var ee = svgElement.getElementsByTagName("a")[i];
            //获取超链接的子元素
            var linkName = "";
            for (var j = 0; j < ee.children.length; j++) {
                linkName += ee.children[j].innerHTML;
            }
            if (ee.getAttribute("xlink:href")) {
                ee.setAttribute("onclick", "showUrl('" + ee.getAttribute("xlink:href") + "','" + linkName + "');");
                ee.setAttribute("xlink:href", "javascript:void(0)");
            }
        }

    }, 100);
}
//==========================备注===============================
var remarkVisbile = false;
/*
   ppt备注显示
*/
function RemarkVisible() {
    remarkVisbile = !remarkVisbile;
    if (remarkVisbile) {
        $('#RemarkDiv').html(ppts[pptIndex].remark);
        $('#RemarkDiv').css("display", "block");

    }
    else {
        $('#RemarkDiv').css("display", "none");
    }
}

//==========================导航===============================
var navVisbile = false;
function NavVisible() {
    navVisbile = !navVisbile;
    if (navVisbile) {
        $('#NavList').css("display", "block");
    }
    else {
        $('#NavList').css("display", "none");
    }
}

/*
   ppt点击导航
*/
function clickNav(index) {
    pptIndex = index;
    showFrame();
    $(" #NavList").css("display", "none");
}

//============================================画图================================================
$(function () {
    InitCancas();
});
var canvas;
function InitCancas() {
    canvas = new fabric.Canvas("freeCanvas", {
        isDrawingMode: true,
        selection: false,
        width: 1500,
        height: 800
    });
}
//清除画板
function clearCanvas() {
    canvas.clear();
}


//==========================================全屏====================================================== 


var isFullScreen = false;
var srcWidth = 0;
var scrHeight = 0;
function toggleFullScreen() {
    //获取浏览器的可用宽度
    isFullScreen = !isFullScreen;
    let width = 0;
    let height = 0;
    if (isFullScreen) {
        $('#pptMax').attr("src","/Content/img/7.png");
        width = $(window).width();
        height = $(window).height()-10;
        if ((height * pptRatio) <= width) {
            width = height * pptRatio;
        }
        else {
            height = width / pptRatio;
        } 
    }
    else {
        $('#pptMax').attr("src", "/Content/img/6.png");
        width = pptContainerWidth;
        height = pptContainerHeight;
    }
    SetDivSize(width, height);
    SetSvgContainer();
}